<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="equipmentno">
        <el-input v-model="queryParams.equipmentno" placeholder="请输入设备编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="隶属编号" prop="wellno">
        <el-input v-model="queryParams.wellno" placeholder="请输入隶属编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出厂日期" prop="exfactorydate">
        <el-date-picker clearable size="small" v-model="queryParams.exfactorydate" type="date" value-format="yyyy-MM-dd" placeholder="选择出厂日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="使用状态" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择使用状态" clearable size="small">
          <el-option v-for="dict in dict.type.enabled" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['dr:equipmentinfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['dr:equipmentinfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['dr:equipmentinfo:remove']">删除</el-button>
      </el-col>
       <el-col :span="1.5">
        <importData :authority="importData_authority" :api="importData_api" :title="importData_title" :templateName="importData_templateName"></importData>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['dr:equipmentinfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--  <el-table-column label="设备ID" align="center" prop="equipmentid" /> -->
      <el-table-column label="设备编号" align="center" prop="equipmentno" />
      <el-table-column label="隶属编号" align="center" prop="wellno" />
      <!--   <el-table-column label="经度坐标" align="center" prop="longitude" />
      <el-table-column label="纬度坐标" align="center" prop="latitude" /> -->
      <el-table-column label="设备名称" align="center" prop="equipmentname" />
      <el-table-column label="出厂日期" align="center" prop="exfactorydate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exfactorydate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备厂商" align="center" prop="equipmentfirm" />
      <!--   <el-table-column label="设备类型" align="center" prop="equipmenttype" /> -->
      <el-table-column label="使用状态" align="center" prop="enabled">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enabled" :value="scope.row.enabled" />
        </template>
      </el-table-column>
      <!--  <el-table-column label="设备概况" align="center" prop="content" /> -->
      <el-table-column label="创建日期" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--  <el-table-column label="更新日期" align="center" prop="updatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['dr:equipmentinfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['dr:equipmentinfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改监测设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body custom-class="devicesDialog">
      <template slot="title">
        <div>
          <i class="el-icon-edit"></i>
          <span> {{title}}</span>
        </div>
      </template>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <table class="table">
          <caption style="font-size:20px;padding:10px 0px">
            监测设备基本信息
          </caption>
          <tr>
            <th class="leibie" rowspan="2">地热井</th>
            <th class="leibie2">名称</th>
            <th>
              <el-select v-model="wellname" filterable placeholder="请搜索隶属地热井" @change="wellnameChange">
                <el-option v-for="item in wellnameOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </th>
            <th class="leibie" rowspan="2">坐标</th>
            <th class="leibie2">经度</th>
            <th>
              <el-input v-model="form.longitude" placeholder="请输入经度坐标" />
            </th>
          </tr>
          <tr>
            <th class="leibie2">编号</th>
            <th>
              <el-input v-model="form.wellno" placeholder="请输入隶属编号" />
            </th>
            <th class="leibie2">纬度</th>
            <th>
              <el-input v-model="form.latitude" placeholder="请输入纬度坐标" />
            </th>
          </tr>
          <tr>
            <th class="leibie" colspan="2">设备名称</th>
            <th>
              <el-input v-model="form.equipmentname" placeholder="请输入设备名称" />
            </th>
            <th class="leibie" colspan="2">设备厂商</th>
            <th>
              <el-input v-model="form.equipmentfirm" placeholder="请输入设备厂商" />
            </th>
          </tr>
          <tr>
            <th class="leibie" colspan="2">设备编号</th>
            <th>
              <el-input v-model="form.equipmentno" placeholder="请输入设备编号" />
            </th>

            <th class="leibie" colspan="2">出厂日期</th>
            <el-date-picker clearable size="small" v-model="form.exfactorydate" type="date" value-format="yyyy-MM-dd" placeholder="选择出厂日期">
            </el-date-picker>
          </tr>
          <tr>
            <th class="leibie" colspan="2">使用状态</th>
            <th>
              <el-select v-model="form.enabled" placeholder="请选择使用状态">
                <el-option v-for="dict in dict.type.enabled" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </th>

            <th class="leibie" colspan="2">录入人</th>
            <th>
              <el-input placeholder="请输入录入人" />
            </th>
          </tr>

          <tr>
            <th class="xmgk" colspan="2">设备概况</th>
            <th colspan="4">
              <el-input v-model="form.content" autosize type="textarea" />
            </th>
          </tr>
        </table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquipmentinfo,
  getEquipmentinfo,
  delEquipmentinfo,
  addEquipmentinfo,
  updateEquipmentinfo,
} from "@/api/geoInfor/equipmentinfo";
import { listGeothermalwellinfo, getGeothermalwellinfo, delGeothermalwellinfo, addGeothermalwellinfo, updateGeothermalwellinfo } from "@/api/geoInfor/geothermalwellinfo";
import ImportData from "@/components/ImportData";
export default {
  name: "Equipmentinfo",
  components: { ImportData },
  dicts: ["enabled"],
  data() {
    return {
      importData_authority: "dr:equipmentinfo:import",
      importData_api: "dr/equipmentinfo/",
      importData_title: "导入监测设备基本信息数据",
      importData_templateName: "监测设备基本信息数据模板",
      wellname: '',
      wellnameOptions: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地热井基本信息表格数据
      geothermalwellinfoList: [],
      // 监测设备信息表格数据
      equipmentinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentno: null,
        wellno: null,
        exfactorydate: null,
        enabled: null,
      },
      // 项目查询参数
      wellQueryParams: {
        wellname: null,
        welltype: null,
        utilizationmode: null,
        enabled: null,
        entryname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getWellList();
  },
  methods: {
    wellnameChange(select) {
      console.log(this.wellname)
      console.log(select)
      this.form.wellno = select;
    },

    /** 查询项目基本信息选择*/
    getWellOptions(geothermalwellinfoList) {
      this.wellnameOptions = geothermalwellinfoList.map((item) => {
        return {
          value: item.wellno,
          label: item.wellname
        }
      });
    },
    /** 查询项目基本信息列表 */
    getWellList() {

      this.loading = true;
      listGeothermalwellinfo(this.wellQueryParams).then(response => {
        this.geothermalwellinfoList = response.rows;
        console.log(this.geothermalwellinfoList)
        this.getWellOptions(this.geothermalwellinfoList);
        this.loading = false;
      });
    },


    /** 查询监测设备信息列表 */
    getList() {
      this.loading = true;
      listEquipmentinfo(this.queryParams).then((response) => {
        this.equipmentinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.wellname = null;
      this.form = {
        equipmentid: null,
        equipmentno: null,
        wellno: null,
        longitude: null,
        latitude: null,
        equipmentname: null,
        exfactorydate: null,
        equipmentfirm: null,
        equipmenttype: null,
        enabled: null,
        content: null,
        createtime: null,
        updatetime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.equipmentid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监测设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const equipmentid = row.equipmentid || this.ids;
      getEquipmentinfo(equipmentid).then((response) => {
        this.form = response.data;
        this.wellname = Number(this.form.wellno);
        /* console.log(this.wellname) */
        this.open = true;
        this.title = "修改监测设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.equipmentid != null) {
            updateEquipmentinfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipmentinfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const equipmentids = row.equipmentid || this.ids;
      this.$modal
        .confirm(
          '是否确认删除监测设备信息编号为"' + equipmentids + '"的数据项？'
        )
        .then(function () {
          return delEquipmentinfo(equipmentids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dr/equipmentinfo/export",
        {
          ...this.queryParams,
        },
        `equipmentinfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.table,
th,
tr {
  border: 1px solid black;
  border-collapse: collapse;
  text-align: center;
}
.xmgk {
  height: 100px;
  background-color: rgb(193, 194, 255, 0.6);
}
.leibie {
  width: 100px;
  background-color: rgb(193, 194, 255, 0.6);
}
.leibie2 {
  width: 100px;
}
</style>
<style lang="scss">
.devicesDialog {
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  .el-dialog__header {
    padding-top: 10px;
    font-size: 18px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    background-color: #ebebeb;
    border-bottom: 1px solid rgb(185, 181, 181) !important;
  }
  .el-dialog__body {
    padding: 5px 40px !important;
  }
}
</style>